package com.bingbing.back.controller;

import com.bingbing.back.bean.SessionList;
import com.bingbing.back.bean.User;
import com.bingbing.back.dto.UserInfoDto;
import com.bingbing.back.mapper.ConsultInfoMapper;
import com.bingbing.back.mapper.SeesionListMapper;
import com.bingbing.back.mapper.UserMapper;
import com.bingbing.back.po.ConsultInfo;
import com.bingbing.back.po.StuInfo;
import com.bingbing.back.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/userinfo")
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService;

    @Autowired(required=false)
    private SeesionListMapper seesionListMapper;

    @Autowired(required=false)
    private UserMapper userMapper;

    @Autowired(required=false)
    private ConsultInfoMapper consultInfoMapper;


    @RequestMapping("/isouruser")
    StuInfo IsOurUser(String userName, String userPwd,String token,String model) {
        StuInfo user = null;

        if(userName==null){
            return null;
        }
        if(token == null){
            return null;
        }
        System.out.println(model);
        if(model.equals("login")) {
            if (userInfoService.SetLoginToken(userName, token) == 0) {
                return null;
            }
        }else if(model.equals("check")){
            if(userInfoService.GetCurrentToken(userName).equals(token)==false){
                return null;
            }
        }

        user = userInfoService.IsOurUser(userName,userPwd);
        return user;
    }

    /********************************************
     函数名：UserReg
     参数：UserInfo 注：自定义类
     返回值：StuInfo  注：自定义类
     功能：判断用户信息是否有效
     说明：若用户重名返回 -1
           若用户重邮箱名返回 -2
           若用户注册失败返回 0
           若用户注册成功返回 1
     若用户信息无效，则返回空值
     模块负责人：杜灿
     近期修改时间：2021-8-28
     ********************************************/
    @RequestMapping("/reg")
    int UserReg(UserInfoDto dto) {
        int status = 0;
        if (userInfoService.UserNameStatus(dto.getUserName()) != null) {
            return -1;
        }
        if (userInfoService.UserEmailStatus(dto.getUserEmail()) != null) {
            return -2;
        }
        status = userInfoService.UserReg(dto);

        User user = userMapper.selectUserByName(dto.getUserName());

        SessionList sessionList = new SessionList();
        sessionList.setUserId(user.getStuId());
        sessionList.setUnReadCount(0);
        sessionList.setUserIdName(dto.getUserName());
        List<ConsultInfo> ConsultInfoList = consultInfoMapper.selectAllConsult();
        System.out.println(ConsultInfoList);
        for (ConsultInfo info : ConsultInfoList) {
        sessionList.setListName(info.getConsultName());
        sessionList.setToUserId(info.getConsultId());
        seesionListMapper.insert(sessionList);
    }

        sessionList.setListName(dto.getUserName());
        sessionList.setToUserId(user.getStuId());
        sessionList.setUnReadCount(0);
        for (ConsultInfo info : ConsultInfoList) {
        sessionList.setUserId(info.getConsultId());
        sessionList.setUserIdName(info.getConsultName());
        seesionListMapper.insert(sessionList);
    }

        return status;
    }

    @RequestMapping("/iscurrentuser")
    int IsCurrentUser(String userName,String token){
        if(token==userInfoService.GetCurrentToken(userName)){
            return 1;
        }
        return 0;
    }
}
